/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theater, canteen, dorm, lab, office, classroom, uproom, downroom;
      
        // create the rooms
        outside = new Room("学校大门。现在有三条路，随便走一条吧。");
        theater = new Room("学校剧场。这里没有人，角落里好像有什么东西。", new Thing("面具", 1));
        canteen = new Room("食堂。你因为睡过头了，急匆匆的赶来学校，什么都没吃，现在肚子很饿，找找有什么东西吃吧。", new Thing("苹果", 1));
        office = new Room("老师办公室。你向老师说明了情况，老师告诉了你教室在二楼。", new Thing("作业", 1));
        downroom = new Room("地下室。教室应该不会在这里，走吧。", new Thing("奇怪的钥匙", 1));
        uproom = new Room("二楼。就快到教室了，继续走吧。");
        lab = new Room("实验室。教室好像不在这一边，往另一边走吧。", new Thing("毒药", 1));
        classroom = new Room("教室。恭喜你终于找到了教室。");
        dorm = new Room("寝室。现在不是来这里的时候，快先去找到教室再说吧。");
        
        // initialise room exits
        outside.setExit("east", theater);
        outside.setExit("north", office);
        outside.setExit("west", canteen);
        theater.setExit("west", outside);
        canteen.setExit("east", outside);
        office.setExit("south", outside);
        office.setExit("north", dorm);
        office.setExit("down", downroom);
        office.setExit("up", uproom);
        uproom.setExit("west", lab);
        uproom.setExit("east", classroom);
        lab.setExit("east", uproom);
        dorm.setExit("south", office);
        classroom.setExit("quit", classroom);
        
        currentRoom = outside;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("感谢您的游玩。再见。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("今天是开学第一天，但是你睡过头了。");
        System.out.println("你急匆匆赶到学校，发现大家都到教室上课去了。");
        System.out.println("你不知道教室在哪，没有人帮你，你只能自己想办法找到教室。");
        System.out.println();
        System.out.println("如果需要帮助，请输入'help'。");
        System.out.println();
        System.out.println("你现在在" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不明白你的意思...");
            return false;
        }
   
        Word commandWord = command.getCommandWord();
        switch(commandWord) {
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pick();
                break;
            case LOOK:
                lookThing(this.currentRoom);
                break;
            case CHECK:
                check();
                break;
            case EAT:
                eats(command);
                break;
        }
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        // else if (commandWord.equals("look")) {
            // lookThing(this.currentRoom);
        // }
        // else if (commandWord.equals("pick")) {
            // pick();
        // }
        // else if (commandWord.equals("check")) {
            // checks();
        // }
        // else if (commandWord.equals("eat")) {
            // eats(command);
        // }

        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你迷路了，你不知道教室在哪。");
        System.out.println("你要找到教室。");
        System.out.println();
        System.out.println("你要做些什么？");
        System.out.println("   " + CommandWords.allCommandWords());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪里？");
            return;
        }
 
        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);

        if (nextRoom == null) {
            System.out.println("这里没有路。");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在在" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
    private void lookThing(Room room) {
        Thing thing = room.getThing();
        if(thing == null) {
            System.out.println("这里没有任何东西。");
        }
        else {
            System.out.println("这里有" + thing + "。");
        }
    }
    
    private void pick() {
        Thing thing = currentRoom.getThing();
        if(thing != null) {
            player.pick(thing);
            currentRoom.removeThing();
            System.out.println("你拿起了" + thing + "。");
        } else {
            System.out.println("没有任何东西可以拿。");
        }
    }
    
    private void check(){
        player.checks();
    }
    
    private void eats(Command command){
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么？");
            return;
        }
        String foot = command.getSecondWord();
        player.eat(foot);
    }
}
